﻿using System.Web.Hosting;
using EPiServer.ImageLibrary;
using OpenWaves.ImageTransformations.Web;

namespace OpenWaves.ImageTransformations.EPiServer
{
    public static class BasicResolverExtensions
    {
         public static BasicResolver RegisterWebImageTransformationModule(this BasicResolver resolver, VirtualPathProvider virtualPathProvider = null, string transformedImagesStorePath = null, IImageTransformationService imageTransformationService = null)
         {
             var module = new WebImageTransformationModuleImplementation(new WebImageTransformationService(
                 new EPiVirtualPathFileProvider(virtualPathProvider ?? HostingEnvironment.VirtualPathProvider),
                 new ConcurrentFileStore(new MapPathBasedFileStore(UrlPath.Parse(transformedImagesStorePath ?? "~/Content/Images/Transformed"))),
                 imageTransformationService ?? new EPiImageTransformationService(new ImageService())),
                 new ForEditorsImageTransformationUrlValidationRule(), 
                 new HmacImageTransformationUrlValidationRule());

             return resolver
                 .Register<IWebImageTransformationService>(module)
                 .Register<IWebImageTransformationModuleImplementation>(module);
         }
    }
}